#include "SPopupWindow.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>

#include <QHBoxLayout>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPushButton>
#include <QStylePainter>
#include <QVBoxLayout>

#include "SImageButton.h"

SPopupWindow::SPopupWindow(QWidget* parent /*= nullptr*/) : QDialog(parent) {
  setWindowFlag(Qt::FramelessWindowHint);
  setAttribute(Qt::WA_TranslucentBackground);

  QVBoxLayout* layout = new QVBoxLayout();  // 窗口布局
  layout->setContentsMargins(0, 0, 0, 0);
  layout->setSpacing(0);

  QHBoxLayout* titleLayout = new QHBoxLayout();  // 标题栏
  titleLayout->setSpacing(0);
  titleLayout->setContentsMargins(0, 1, 1, 0);
  titleLayout->setAlignment(Qt::AlignTop);
  layout->addLayout(titleLayout);

  QWidget* dumyWidget = new QWidget(this);
  dumyWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  dumyWidget->setFixedHeight(m_nTitleHeight);
  titleLayout->addWidget(dumyWidget);

  m_btnClose = new SImageButton(this, "widgets_win_close");
  titleLayout->addWidget(m_btnClose);

  QVBoxLayout* layoutMain =
      new QVBoxLayout();  // 要显示的元素布局,包含内容控件和主要的按钮
  layout->addLayout(layoutMain);
  layoutMain->setContentsMargins(8, 0, 8, 8);
  layoutMain->setSpacing(0);

  m_contentLayout = new QVBoxLayout();
  m_contentLayout->setContentsMargins(0, 0, 0, 0);
  m_contentLayout->setSpacing(0);
  layoutMain->addLayout(m_contentLayout);
  layoutMain->addStretch();  // 撑起来

  m_btnLayout = new QHBoxLayout();
  m_btnLayout->setContentsMargins(0, 36, 0, 0);
  m_btnLayout->setSpacing(8);
  m_btnLayout->setDirection(QBoxLayout::RightToLeft);
  layoutMain->addLayout(m_btnLayout);

  setLayout(layout);

  connect(m_btnClose, &SImageButton::clicked, this, &QDialog::reject);
}

void SPopupWindow::setTitle(const QString& v) { m_title = v; }

QPushButton* SPopupWindow::addBtn(QMessageBox::StandardButton btnType,
                                  const QString& caption) {
  Q_ASSERT_X(m_btnLayout->isEmpty(), "SPopupWindow::addBtn",
             "请在显示前添加！");
  if (!m_btnLayout->isEmpty())  // 已经布局，不能再添加
    return nullptr;

  switch (btnType) {
    case QMessageBox::Ok:
      m_btnOK = new QPushButton(caption, this);
      m_btnOK->setFixedSize(120, 24);  // TODO: check size?
      return m_btnOK;
      break;
    case QMessageBox::Cancel:
      m_btnCancel = new QPushButton(caption, this);
      m_btnCancel->setObjectName("grey");
      m_btnCancel->setFixedSize(80, 24);
      return m_btnCancel;
      break;
    case QMessageBox::Apply:
      m_btnApply = new QPushButton(caption, this);
      m_btnApply->setFixedSize(80, 24);
      return m_btnApply;
      break;
    default:
      Q_ASSERT_X(m_btnLayout->isEmpty(), "SPopupWindow::addBtn",
                 "仅支持OK Cancel, Apply！");
      break;
  }

  return nullptr;
}

QPushButton* SPopupWindow::changeBtnName(QMessageBox::StandardButton btnType,
                                         const QString& caption) {
  // Q_ASSERT_X(!m_btnLayout->isEmpty(), "SPopupWindow::changeBtnName",
  // "请在修改前添加！"); if (m_btnLayout->isEmpty())//已经布局，不能再添加
  //	return nullptr;

  switch (btnType) {
    case QMessageBox::Ok:
      if (m_btnOK == nullptr) return nullptr;
      m_btnOK->setText(caption);
      return m_btnOK;
      break;
    case QMessageBox::Cancel:
      if (m_btnCancel == nullptr) return nullptr;
      m_btnCancel->setText(caption);
      return m_btnCancel;
      break;
    case QMessageBox::Apply:
      if (m_btnApply == nullptr) return nullptr;
      m_btnApply->setText(caption);
      return m_btnApply;
      break;
    default:
      Q_ASSERT_X(!m_btnLayout->isEmpty(), "SPopupWindow::changeBtnName",
                 "仅支持OK Cancel, Apply, Change button's Name");
      break;
  }
  return nullptr;
}

static QPoint s_oldPos;
void SPopupWindow::keyPressEvent(QKeyEvent* e) {
  if (e->key() == Qt::Key_Escape) reject();
}

void SPopupWindow::mousePressEvent(QMouseEvent* event) {
  if (event->button() == Qt::LeftButton) {
    setCursor(Qt::SizeAllCursor);
    s_oldPos = event->globalPos();
    m_dragging = true;
  }
}

void SPopupWindow::mouseReleaseEvent(QMouseEvent* event) {
  if (event->button() == Qt::LeftButton) {
    setCursor(Qt::ArrowCursor);
    m_dragging = false;
  }
}

void SPopupWindow::mouseMoveEvent(QMouseEvent* event) {
  if (event->buttons() & Qt::LeftButton && m_dragging) {
    QPoint offset = event->globalPos() - s_oldPos;
    move(frameGeometry().topLeft() + offset);
    s_oldPos = event->globalPos();
  }
}

void SPopupWindow::paintEvent(QPaintEvent* event) {
  QBrush brush(StyleMgr::instance().color("BorderNormal2"));
  QPen pen(StyleMgr::instance().color("BorderNormal2"));
  int r = StyleMgr::instance().size("RoundCorner");

  QStylePainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  painter.setBrush(brush);
  painter.setPen(pen);
  painter.drawRoundedRect(rect(), r, r);

  painter.setPen(QPen(StyleMgr::instance().color("BorderNormal3")));
  painter.drawRoundedRect(rect(), r - 1, r - 1);

  QRect rcBack = rect();
  painter.fillRect(rcBack.adjusted(2, 24, -2, -2),
                   StyleMgr::instance().color("BasicPlate"));

  QRect titleR = rect();
  titleR.setHeight(24);
  titleR.adjust(2, 2, -2, 0);
  painter.fillRect(titleR, StyleMgr::instance().color("MainTitleBack"));
  painter.setPen(StyleMgr::instance().color("TextNormal"));
  painter.drawText(titleR, m_title, QTextOption(Qt::AlignCenter));
}

void SPopupWindow::resizeEvent(QResizeEvent*) {
  if (!m_btnLayout->isEmpty()) return;

  if (m_btnOK) m_btnLayout->addWidget(m_btnOK);
  if (m_btnCancel) m_btnLayout->addWidget(m_btnCancel);
  m_btnLayout->addStretch();
  if (m_btnApply) m_btnLayout->addWidget(m_btnApply);
}
